package com.example.forum.controller;

import com.example.forum.constant.MessageConstant;
import com.example.forum.entity.User;
import com.example.forum.exception.CustomException;
import com.example.forum.result.Result;
import com.example.forum.services.impl.MessageServiceImpl;
import com.example.forum.services.impl.UserServiceImpl;
import com.example.forum.vo.MessageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.forum.constant.MessageConstant.USER_SESSION;

@Slf4j
@Tag(name = "消息接口")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;

    @Autowired
    private UserServiceImpl userService;

    /**
     * 发送消息
     *
     * @param receiveUserId 接收者id
     * @param content       消息内容
     * @return Result
     */
    @Operation(summary = "发送消息")
    @PostMapping("/send")
    public Result send(HttpServletRequest request,
                       @Parameter(description = "接收者id") @RequestParam(value = "receiveUserId") @NotNull Long receiveUserId,
                       @Parameter(description = "消息内容") @RequestParam(value = "content") @NotNull String content) {
        try {
            // 判断接收者是否存在
            userService.getUser(receiveUserId);

            // 获取发送者信息
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(USER_SESSION);

            // 判断发送者是否登录
            if (user == null) {
                return Result.error(MessageConstant.USER_NOT_LOGIN);
            }
            // 获取发送者id
            Long postUserId = user.getId();

            // 发送消息
            messageService.send(receiveUserId, content, postUserId);

            return Result.success();

        } catch (CustomException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有消息
     *
     * @return Result
     */
    @Operation(summary = "获取所有消息")
    @GetMapping("/getAll")
    public Result<List<MessageVO>> getAll(HttpServletRequest request) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(USER_SESSION);

        // 判断用户是否登录
        if (user == null) {
            return Result.error(MessageConstant.USER_NOT_LOGIN);
        }

        // 获取用户id
        Long userId = user.getId();
        try {
            return Result.success(messageService.getAll(userId));
        } catch (CustomException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取未读消息数量
     *
     * @return Result
     */
    @Operation(summary = "获取未读消息数量")
    @GetMapping("/getUnreadCount")
    public Result<Integer> getUnreadCount(HttpServletRequest request) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(USER_SESSION);

        // 判断用户是否登录
        if (user == null) {
            return Result.error(MessageConstant.USER_NOT_LOGIN);
        }

        // 获取用户id
        Long userId = user.getId();

        try {
            Integer count = messageService.getUnreadCount(userId);
            return Result.success(count);
        } catch (CustomException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 标记消息已读
     *
     * @return Result
     */
    @Operation(summary = "标记消息已读")
    @PostMapping("/markRead")
    public Result markRead(HttpServletRequest request,
                           @Parameter(description = "消息id") @RequestParam(value = "id") @NotNull Long id) {
        try {
            // 获取用户信息
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(USER_SESSION);

            // 判断用户是否登录
            if (user == null) {
                return Result.error(MessageConstant.USER_NOT_LOGIN);
            }

            // 获取用户id
            Long userId = user.getId();

            // 标记消息已读
            messageService.markRead(id, userId);

            return Result.success();
        } catch (CustomException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 回复消息
     *
     * @return Result
     */
    @Operation(summary = "回复消息")
    @PostMapping("/reply")
    public Result reply(HttpServletRequest request,
                        @Parameter(description = "回复的消息id") @RequestParam(value = "repliedId") Long repliedId,
                        @Parameter(description = "回复的内容") @RequestParam(value = "content") String content) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(USER_SESSION);

        // 判断用户是否登录
        if (user == null) {
            return Result.error(MessageConstant.USER_NOT_LOGIN);
        }

        // 获取用户id
        Long userId = user.getId();
        try {
            messageService.reply(repliedId, content, userId);
            return Result.success();
        } catch (CustomException e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
